// 导入依赖
import {createStore} from 'redux'
// 初始化数据
export const initState = {
  age: 18,
  name: 'Tom'
}
// 通过纯函数修改state
// 统一管理action的type值
export const Types = {
  AGEADD: 'AGEADD',
  NAMECHANGE: 'NAMECHANGE'
}
// 统一管理action-action的生成器
export const actionCreator = {
  ageAdd: ()=>({type: Types.AGEADD}),
  nameChange: (name)=>({type: Types.NAMECHANGE, name: name}),
}
// 1. 根据参数可以确定返回值；2.没有副作用，没有读写操作
export const reducer = (state=initState, action)=>{
  // console.log(action);
  switch (action.type) {
    case Types.AGEADD:
      return {
        ...state,
        age: state.age + 1
      }
    case Types.NAMECHANGE:
      return {
        ...state,
        name: action.name
      }
  
    default:
      return state
  }
}
// 创建仓库
const store = createStore(reducer);
// 暴露
export default store